<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>relative_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_date_relative_test.js.html">relative_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.LOCALE = &#39;en_US&#39;;
<a name="line16"></a>goog.provide(&#39;goog.date.relativeTest&#39;);
<a name="line17"></a>goog.setTestOnly(&#39;goog.date.relativeTest&#39;);
<a name="line18"></a>
<a name="line19"></a>goog.require(&#39;goog.date.DateTime&#39;);
<a name="line20"></a>goog.require(&#39;goog.date.relative&#39;);
<a name="line21"></a>goog.require(&#39;goog.i18n.DateTimeFormat&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line23"></a>
<a name="line24"></a>// Timestamp to base times for test on.
<a name="line25"></a>var baseTime = new Date(2009, 2, 23, 14, 31, 6).getTime();
<a name="line26"></a>
<a name="line27"></a>// Ensure goog.now returns a constant timestamp.
<a name="line28"></a>goog.now = function() {
<a name="line29"></a>  return baseTime;
<a name="line30"></a>};
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>function testFormatRelativeForPastDates() {
<a name="line34"></a>  var fn = goog.date.relative.format;
<a name="line35"></a>
<a name="line36"></a>  assertEquals(&#39;Should round seconds to the minute below&#39;,
<a name="line37"></a>      &#39;0 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 14:30:10&#39;)));
<a name="line38"></a>
<a name="line39"></a>  assertEquals(&#39;Should round seconds to the minute below&#39;,
<a name="line40"></a>      &#39;1 minute ago&#39;,
<a name="line41"></a>      fn(timestamp(&#39;23 March 2009 14:29:56&#39;)));
<a name="line42"></a>
<a name="line43"></a>  assertEquals(&#39;Should round seconds to the minute below&#39;,
<a name="line44"></a>      &#39;2 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 14:29:00&#39;)));
<a name="line45"></a>
<a name="line46"></a>  assertEquals(&#39;10 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 14:20:10&#39;)));
<a name="line47"></a>  assertEquals(&#39;59 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 13:31:42&#39;)));
<a name="line48"></a>  assertEquals(&#39;2 hours ago&#39;, fn(timestamp(&#39;23 March 2009 12:20:56&#39;)));
<a name="line49"></a>  assertEquals(&#39;23 hours ago&#39;, fn(timestamp(&#39;22 March 2009 15:30:56&#39;)));
<a name="line50"></a>  assertEquals(&#39;1 day ago&#39;, fn(timestamp(&#39;22 March 2009 12:11:04&#39;)));
<a name="line51"></a>  assertEquals(&#39;1 day ago&#39;, fn(timestamp(&#39;22 March 2009 00:00:00&#39;)));
<a name="line52"></a>  assertEquals(&#39;2 days ago&#39;, fn(timestamp(&#39;21 March 2009 23:59:59&#39;)));
<a name="line53"></a>  assertEquals(&#39;2 days ago&#39;, fn(timestamp(&#39;21 March 2009 10:30:56&#39;)));
<a name="line54"></a>  assertEquals(&#39;2 days ago&#39;, fn(timestamp(&#39;21 March 2009 00:00:00&#39;)));
<a name="line55"></a>  assertEquals(&#39;3 days ago&#39;, fn(timestamp(&#39;20 March 2009 23:59:59&#39;)));
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>function testFormatRelativeForFutureDates() {
<a name="line59"></a>  var fn = goog.date.relative.format;
<a name="line60"></a>
<a name="line61"></a>  assertEquals(&#39;Should round seconds to the minute below&#39;,
<a name="line62"></a>      &#39;in 1 minute&#39;,
<a name="line63"></a>      fn(timestamp(&#39;23 March 2009 14:32:05&#39;)));
<a name="line64"></a>
<a name="line65"></a>  assertEquals(&#39;Should round seconds to the minute below&#39;,
<a name="line66"></a>      &#39;in 2 minutes&#39;, fn(timestamp(&#39;23 March 2009 14:33:00&#39;)));
<a name="line67"></a>
<a name="line68"></a>  assertEquals(&#39;in 10 minutes&#39;, fn(timestamp(&#39;23 March 2009 14:40:10&#39;)));
<a name="line69"></a>  assertEquals(&#39;in 59 minutes&#39;, fn(timestamp(&#39;23 March 2009 15:29:15&#39;)));
<a name="line70"></a>  assertEquals(&#39;in 2 hours&#39;, fn(timestamp(&#39;23 March 2009 17:20:56&#39;)));
<a name="line71"></a>  assertEquals(&#39;in 23 hours&#39;, fn(timestamp(&#39;24 March 2009 13:30:56&#39;)));
<a name="line72"></a>  assertEquals(&#39;in 1 day&#39;, fn(timestamp(&#39;24 March 2009 14:31:07&#39;)));
<a name="line73"></a>  assertEquals(&#39;in 1 day&#39;, fn(timestamp(&#39;24 March 2009 16:11:04&#39;)));
<a name="line74"></a>  assertEquals(&#39;in 1 day&#39;, fn(timestamp(&#39;24 March 2009 23:59:59&#39;)));
<a name="line75"></a>  assertEquals(&#39;in 2 days&#39;, fn(timestamp(&#39;25 March 2009 00:00:00&#39;)));
<a name="line76"></a>  assertEquals(&#39;in 2 days&#39;, fn(timestamp(&#39;25 March 2009 10:30:56&#39;)));
<a name="line77"></a>  assertEquals(&#39;in 2 days&#39;, fn(timestamp(&#39;25 March 2009 23:59:59&#39;)));
<a name="line78"></a>  assertEquals(&#39;in 3 days&#39;, fn(timestamp(&#39;26 March 2009 00:00:00&#39;)));
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>function testFormatPast() {
<a name="line83"></a>  var fn = goog.date.relative.formatPast;
<a name="line84"></a>
<a name="line85"></a>  assertEquals(&#39;59 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 13:31:42&#39;)));
<a name="line86"></a>  assertEquals(&#39;0 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 14:32:05&#39;)));
<a name="line87"></a>  assertEquals(&#39;0 minutes ago&#39;, fn(timestamp(&#39;23 March 2009 14:33:00&#39;)));
<a name="line88"></a>  assertEquals(&#39;0 minutes ago&#39;, fn(timestamp(&#39;25 March 2009 10:30:56&#39;)));
<a name="line89"></a>}
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>function testFormatDay() {
<a name="line93"></a>  var fn = goog.date.relative.formatDay;
<a name="line94"></a>  var formatter = new goog.i18n.DateTimeFormat(
<a name="line95"></a>      goog.i18n.DateTimeFormat.Format.SHORT_DATE);
<a name="line96"></a>  var format = goog.bind(formatter.format, formatter);
<a name="line97"></a>
<a name="line98"></a>  assertEquals(&#39;Sep 25&#39;, fn(timestamp(&#39;25 September 2009 10:31:06&#39;)));
<a name="line99"></a>  assertEquals(&#39;Mar 25&#39;, fn(timestamp(&#39;25 March 2009 00:12:19&#39;)));
<a name="line100"></a>  assertEquals(&#39;Tomorrow&#39;, fn(timestamp(&#39;24 March 2009 10:31:06&#39;)));
<a name="line101"></a>  assertEquals(&#39;Tomorrow&#39;, fn(timestamp(&#39;24 March 2009 00:12:19&#39;)));
<a name="line102"></a>  assertEquals(&#39;Today&#39;, fn(timestamp(&#39;23 March 2009 10:31:06&#39;)));
<a name="line103"></a>  assertEquals(&#39;Today&#39;, fn(timestamp(&#39;23 March 2009 00:12:19&#39;)));
<a name="line104"></a>  assertEquals(&#39;Yesterday&#39;, fn(timestamp(&#39;22 March 2009 23:48:12&#39;)));
<a name="line105"></a>  assertEquals(&#39;Yesterday&#39;, fn(timestamp(&#39;22 March 2009 04:11:23&#39;)));
<a name="line106"></a>  assertEquals(&#39;Mar 21&#39;, fn(timestamp(&#39;21 March 2009 15:54:45&#39;)));
<a name="line107"></a>  assertEquals(&#39;Mar 19&#39;, fn(timestamp(&#39;19 March 2009 01:22:11&#39;)));
<a name="line108"></a>
<a name="line109"></a>  // Test that a formatter can also be accepted as input.
<a name="line110"></a>  assertEquals(&#39;Today&#39;, fn(timestamp(&#39;23 March 2009 10:31:06&#39;), format));
<a name="line111"></a>  assertEquals(&#39;Today&#39;, fn(timestamp(&#39;23 March 2009 00:12:19&#39;), format));
<a name="line112"></a>  assertEquals(&#39;Yesterday&#39;, fn(timestamp(&#39;22 March 2009 23:48:12&#39;), format));
<a name="line113"></a>  assertEquals(&#39;Yesterday&#39;, fn(timestamp(&#39;22 March 2009 04:11:23&#39;), format));
<a name="line114"></a>  assertEquals(format(gdatetime(timestamp(&#39;21 March 2009 15:54:45&#39;))),
<a name="line115"></a>      fn(timestamp(&#39;21 March 2009 15:54:45&#39;), format));
<a name="line116"></a>  assertEquals(format(gdatetime(timestamp(&#39;19 March 2009 01:22:11&#39;))),
<a name="line117"></a>      fn(timestamp(&#39;19 March 2009 01:22:11&#39;), format));
<a name="line118"></a>}
<a name="line119"></a>
<a name="line120"></a>function testGetDateString() {
<a name="line121"></a>  var fn = goog.date.relative.getDateString;
<a name="line122"></a>
<a name="line123"></a>  assertEquals(&#39;2:21 PM (10 minutes ago)&#39;,
<a name="line124"></a>               fn(new Date(baseTime - 10 * 60 * 1000)));
<a name="line125"></a>  assertEquals(&#39;4:31 AM (10 hours ago)&#39;,
<a name="line126"></a>               fn(new Date(baseTime - 10 * 60 * 60 * 1000)));
<a name="line127"></a>  assertEquals(&#39;Friday, March 13, 2009 (10 days ago)&#39;,
<a name="line128"></a>               fn(new Date(baseTime - 10 * 24 * 60 * 60 * 1000)));
<a name="line129"></a>  assertEquals(&#39;Tuesday, March 3, 2009&#39;,
<a name="line130"></a>               fn(new Date(baseTime - 20 * 24 * 60 * 60 * 1000)));
<a name="line131"></a>
<a name="line132"></a>  // Test that goog.date.DateTime can also be accepted as input.
<a name="line133"></a>  assertEquals(&#39;2:21 PM (10 minutes ago)&#39;,
<a name="line134"></a>               fn(gdatetime(baseTime - 10 * 60 * 1000)));
<a name="line135"></a>  assertEquals(&#39;4:31 AM (10 hours ago)&#39;,
<a name="line136"></a>               fn(gdatetime(baseTime - 10 * 60 * 60 * 1000)));
<a name="line137"></a>  assertEquals(&#39;Friday, March 13, 2009 (10 days ago)&#39;,
<a name="line138"></a>               fn(gdatetime(baseTime - 10 * 24 * 60 * 60 * 1000)));
<a name="line139"></a>  assertEquals(&#39;Tuesday, March 3, 2009&#39;,
<a name="line140"></a>               fn(gdatetime(baseTime - 20 * 24 * 60 * 60 * 1000)));
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>function testGetPastDateString() {
<a name="line144"></a>  var fn = goog.date.relative.getPastDateString;
<a name="line145"></a>  assertEquals(&#39;2:21 PM (10 minutes ago)&#39;,
<a name="line146"></a>               fn(new Date(baseTime - 10 * 60 * 1000)));
<a name="line147"></a>  assertEquals(&#39;2:41 PM (0 minutes ago)&#39;,
<a name="line148"></a>               fn(new Date(baseTime + 10 * 60 * 1000)));
<a name="line149"></a>
<a name="line150"></a>  // Test that goog.date.DateTime can also be accepted as input.
<a name="line151"></a>  assertEquals(&#39;2:21 PM (10 minutes ago)&#39;,
<a name="line152"></a>               fn(gdatetime(baseTime - 10 * 60 * 1000)));
<a name="line153"></a>  assertEquals(&#39;2:41 PM (0 minutes ago)&#39;,
<a name="line154"></a>               fn(gdatetime(baseTime + 10 * 60 * 1000)));
<a name="line155"></a>}
<a name="line156"></a>
<a name="line157"></a>function gdatetime(timestamp) {
<a name="line158"></a>  return new goog.date.DateTime(new Date(timestamp));
<a name="line159"></a>}
<a name="line160"></a>
<a name="line161"></a>function timestamp(str) {
<a name="line162"></a>  return new Date(str).getTime();
<a name="line163"></a>}
</pre>


</body>
</html>
